﻿/*
===========================================================================
Immemorsion LGPL Source code
Copyright trszdev - rahimyanov@gmail.com

This file is part of Immemorsion.

Immemorsion is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Immemorsion is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with Immemorsion.  If not, see <http://www.gnu.org/licenses/>.
===========================================================================
*/
using System.Linq;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace Immemorsion
{
    public class ExactByteMemoryPattern : ByteMemoryPattern
    {

        private byte[] Bytes;
        private static MaskedByte[] BytesToPattern(byte[] bytes)
        {
            var result = bytes
                .Select(a => new MaskedByte(a, ByteMask.Exact))
                .ToArray();
            return result;
        }

        public ExactByteMemoryPattern(byte[] bytes)
            : base(BytesToPattern(bytes))
        {
            Bytes = bytes;
        }

        public override bool Suits(byte[] bytes)
        {
            for (var i = 0; i < Bytes.Length; i++)
            {
                if (Bytes[i] != bytes[i])
                    return false;
            }
            return true; 
        }

        public override unsafe bool Suits(byte* bytePointer)
        {
            for (var i = 0; i < Bytes.Length; i++)
            {
                if (Bytes[i] != *(bytePointer+i))
                    return false;
            }
            return true;
        }

    }
}
